//
//  StaticTools+UIOperation.m
//  JFmerchant
//
//  Created by 文彬 on 2017/12/29.
//  Copyright © 2017年 文彬. All rights reserved.
//

#import "StaticTools+UIOperation.h"
#import "LoginViewController.h"
#import "PickerViewController.h"

@implementation StaticTools (UIOperation)

/**
 导航栏右侧增加操作按钮
 
 @param title 按钮标题
 @param color 按钮文字颜色
 @param font  文字字体
 @param handle 点击回调
 */
+ (void)addNavgationRightButtonWithController:(UIViewController*)controller
                                        title:(NSString*)title
                                         font:(UIFont*)font
                                        color:(UIColor*)color
                                        click:(CompleteHandle)handle{
    
    float width = [StaticTools getLabelWidth:title defautHeight:40 fontSize:font];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.titleLabel.font = font;
    button.frame = CGRectMake(0, 0, width+10, 40);
    controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    [button buttonClick:^{
        if (handle!=nil) {
            handle(nil);
        }
    }];
}

/**
 在view底部增加一操作按钮
 
 @param view table
 @param title 按钮文字
 @param font 按钮字体
 @param backgroundColor 按钮背景颜色
 @param titleColor 文字颜色
 @param buttonHeight 按钮高度
 @param bottomDisance 按钮距离底部距离
 @param leftDistance 按钮距离左侧距离  右侧距离默认和左侧一样
 @param cornerRadius 圆角
 @param handle 点击回调
 */
+ (void)addPageFootButton:(UIView*)view
                    title:(NSString*)title
                titleFont:(UIFont*)font
          backgroundColor:(UIColor*)backgroundColor
               titleColor:(UIColor*)titleColor
             buttonHeight:(float)buttonHeight
             bottomMargin:(float)bottomDisance
               leftMargin:(float)leftDistance
             cornerRadius:(NSInteger)cornerRadius
                    click:(CompleteHandle)handle{
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:title forState:UIControlStateNormal];
    nextBtn.backgroundColor = backgroundColor;
    [nextBtn setTitleColor:titleColor forState:UIControlStateNormal];
    nextBtn.titleLabel.font = font;
    nextBtn.layer.cornerRadius = cornerRadius;
    [view addSubview:nextBtn];
    [nextBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(leftDistance));
        make.right.equalTo(@(-leftDistance));
        make.bottom.equalTo(@(-bottomDisance));
        make.height.equalTo(@(buttonHeight));
    }];
    
    [nextBtn buttonClick:^{
        
        if (handle!=nil) {
            handle(nil);
        }
    }];
}

/**
 在table底部增加一操作按钮
 
 @param tableView table
 @param title 按钮文字
 @param font 按钮字体
 @param backgroundColor 按钮背景颜色
 @param titleColor 文字颜色
 @param buttonHeight 按钮高度
 @param topDisance 按钮距离table底部距离
 @param leftDistance 按钮距离左侧距离  右侧距离默认和左侧一样
 @param handle 点击回调
 */
+ (void)addTableViewFootButton:(UITableView*)tableView
                         title:(NSString*)title
                     titleFont:(UIFont*)font
               backgroundColor:(UIColor*)backgroundColor
                    titleColor:(UIColor*)titleColor
                  buttonHeight:(float)buttonHeight
                     topMargin:(float)topDisance
                    leftMargin:(float)leftDistance
                         click:(CompleteHandle)handle{
    
    //!!! 得加上这句 否则得到的tableview宽度是0
    [tableView layoutIfNeeded];
    
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, topDisance+buttonHeight+10)];
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:title forState:UIControlStateNormal];
    nextBtn.backgroundColor = backgroundColor;
    [nextBtn setTitleColor:titleColor forState:UIControlStateNormal];
    nextBtn.titleLabel.font = font;
    nextBtn.layer.cornerRadius = 5;
    [footView addSubview:nextBtn];
    [nextBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(leftDistance));
        make.width.equalTo(@(tableView.frame.size.width-2*leftDistance));
        make.bottom.equalTo(@0);
        make.height.equalTo(@(buttonHeight));
    }];
    
    [nextBtn buttonClick:^{
        
        if (handle!=nil) {
            handle(nil);
        }
    }];
    
    
    tableView.tableFooterView = footView;
    
}

/**
 弹出支付方式选择
 
 @param handle 选择回调
 */
+ (void)showPayType:(CompleteHandle)handle
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgView.backgroundColor = RGBACOLOR(0, 0, 0, 0.3);
    [ApplicationDelegate.window addSubview:bgView];
    
    UIView *opeView = [[UIView alloc]init];
    opeView.backgroundColor = [UIColor whiteColor];
    opeView.layer.cornerRadius = 5;
    [bgView addSubview:opeView];
    [opeView makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@300);
        make.height.equalTo(@220);
        make.centerY.equalTo(bgView.centerY);
        make.centerX.equalTo(bgView.centerX);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.textColor = kMainOriginColor;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"选择收款方式";
    [opeView addSubview:titleLabel];
    [titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@15);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.height.equalTo(@30);
    }];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = kLineColor;
    [opeView addSubview:lineView];
    [lineView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.bottom).offset(10);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.height.equalTo(@0.5);
    }];
    
    for (int i=0; i<2; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [opeView addSubview:button];
        [button makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@(i==0?55:110));
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.height.equalTo(@55);
        }];
        [button buttonClick:^{
            [bgView removeFromSuperview];
            if (handle!=nil) {
                handle(@(i));
            }
        }];
        
        UIImageView *logoImgView = [[UIImageView alloc]init];
        logoImgView.image = [UIImage imageNamed:@"zhifubao_"];
        [opeView addSubview:logoImgView];
        [logoImgView makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@30);
            make.height.equalTo(@30);
            make.centerY.equalTo(button.centerY);
            make.right.equalTo(button.centerX).offset(-15);
        }];
        
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.text = i==0?@"支付宝":@"银行卡";
        [opeView addSubview:titleLabel];
        [titleLabel makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(button.centerY);
            make.left.equalTo(button.centerX).offset(-5);
            make.right.equalTo(@0);
            make.height.equalTo(@30);
        }];
        
        UIView *lineView = [[UIView alloc]init];
        lineView.backgroundColor = kLineColor;
        [button addSubview:lineView];
        [lineView makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(@0);
            make.left.equalTo(@0);
            make.right.equalTo(@0);
            make.height.equalTo(@0.5);
        }];
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button setTitleColor:kTitleColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [opeView addSubview:button];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.height.equalTo(@55);
    }];
    [button buttonClick:^{
        [bgView removeFromSuperview];
        if (handle!=nil) {
            handle(@2);
        }
    }];
}

/**
 弹出登录页面
 
 @param handle 成功回调
 */
+ (void)showLoginSuccess:(CompleteHandle)handle
{
    LoginViewController *loginController = [[LoginViewController alloc]init];
    [ApplicationDelegate.window.rootViewController presentViewController:loginController animated:YES completion:nil];
}


/**
 弹出picer选择页面

 @param datas 数据源  数组类型  里面单个数据项也未数组  例如想要两个component时  datas里就放两个数组
 @param handle 完成回调 数组类型  单选数据为每个component选择的下标
 */
+ (void)showPickerController:(NSArray*)datas complete:(CompleteHandle)handle{
    
    PickerViewController *picker =[[PickerViewController alloc]init];
    picker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    picker.datas = datas;
    picker.doneHandle = handle;
    [ApplicationDelegate.window.rootViewController presentViewController:picker animated:YES completion:nil];
}

@end
